/*
 * GET home page.
 */

 
//加好友请求
exports.requestFriends = function(req , res){
		var keycode = req.body.key;
		var keycode = aes.decode(keycode.replace(' ','+'),key);
		var keycode = keycode.split('|');
		var mobile = req.body.mobile;
		var theTime = parseInt((new Date()).valueOf()/1000);
		if(keycode[2] && mobile){		//验证密钥
			if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(mobile)))
				res.end(JSON.stringify({code:'2'}));
			else{
				var async = require('async');
				async.waterfall([
					function(callback){
						var mysql = require('mysql');
						var client = mysql.createConnection(myDb);
						client.connect();
						client.query("use " + myDb.DbName);
						//console.log('SELECT `id` FROM 360_member WHERE mobile = "'+ mobile +'" limit 1');
						client.query('SELECT `id` FROM 360_member WHERE mobile = "'+ mobile +'" limit 1',
							function selectCb(err, returnData){
								if(err)
									res.send(JSON.stringify({code:'3'}));
								if(returnData.length){
									callback(null,returnData[0].id);
								}
								else{
									res.send(JSON.stringify({code:'2'}));
									return false;
								}
							}
						)
					},
					function(a1 , callback){
						var mysql = require('mysql');
						var client = mysql.createConnection(myDb);
						client.connect();
						client.query("use " + myDb.DbName);						
						client.query("INSERT INTO `360_member_relation` (`id`, `uid`, `fid`, `state` ,`addtime`) VALUES (NULL, '"+keycode[2]+"', '"+a1+"', '1' ,"+theTime+")",
							function selectCb(err, insertState, fields){
								//console.log("INSERT INTO `360_member_relation` (`id`, `uid`, `fid`, `state`) VALUES (NULL, '"+keycode[2]+"', '"+a1+"', '1')");
								//console.log(insertState);
								if(!err){
									res.send(JSON.stringify({code:'1'}));
									return false;								
								}
								else
									res.send(JSON.stringify({code:'3'}));
							}	
						)
					}],
					function (err, result) {
						return false;
					}
				)				
			}
		}
		else
			res.end(JSON.stringify({code:'3'}));
}

//接受好友请求
exports.acceptFriend = function(req , res){
	var keycode = req.body.key;
	var keycode = aes.decode(keycode.replace(' ','+'),key);
	var keycode = keycode.split('|');
	var cid = req.body.cid * 1;
	var type = !req.body.type ? "2": "3";
	if(keycode[2]){		//验证密钥
		var mysql = require('mysql');
		var client = mysql.createConnection(myDb);
		client.connect();
		client.query("use " + myDb.DbName);
		console.log("UPDATE `360_member_relation` SET  `state` =  '"+type+"' WHERE `fid` ="+keycode[2]+" and `state`=1 and `uid` =" +cid+ " LIMIT 1");
		client.query("UPDATE `360_member_relation` SET  `state` =  '"+type+"' WHERE `fid` ="+keycode[2]+" and `uid` =" +cid+ " and `state`=1",
			function selectCb(err, upState, fields){
				if(upState.affectedRows)
					res.send(JSON.stringify({code:'1'}));								
				else
					res.send(JSON.stringify({code:'2'}));
			}	
		)		
	}
	else
		res.end(JSON.stringify({code:'2'}));
}


//好友资料
exports.friendData = function(req , res){
	var keycode = req.body.key;
	var keycode = aes.decode(keycode.replace(' ','+'),key);
	var keycode = keycode.split('|');
	var cid = req.body.cid * 1;
	if(keycode[2]){		//验证密钥
		var mysql = require('mysql');
		var client = mysql.createConnection(myDb);
		client.connect();
		client.query("use " + myDb.DbName);	
		var async = require('async');
			async.waterfall([
				function(callback){					
					client.query('SELECT `id` FROM 360_member_relation WHERE ((`uid`= '+keycode[2]+' and `fid` = '+cid+')  or (`fid`= '+keycode[2]+' and `uid` = '+cid+')) and `state`=2 limit 1',
						function selectCb(err, returnData){
							if(err)
								res.send(JSON.stringify({code:'2'}));
							else{
								if(returnData.length)
									callback(null,true);
								else
									res.send(JSON.stringify({code:'2'}));
							}
						}
					)
				},
				function(a1 , callback){						
					client.query("SELECT `headPath`,`nickName`,`area`,`sex`,`signature` FROM  `360_member` where id = "+cid+" LIMIT 1",
						function insertCb(err, row, fields){
							if(!err){
								var data = {};
								data.code =  "1";
								data.data =  row;
								res.send(JSON.stringify(data));
								return false;								
							}
							else
								res.send(JSON.stringify({code:'2'}));
						}	
					)
				}],
				function (err, result) {
					return false;
				}
			)		
	}
	else
		res.end(JSON.stringify({code:'2'}));
}



//好友列表
exports.friendList = function(req , res){
	var keycode = req.body.key;
	var keycode = aes.decode(keycode.replace(' ','+'),key);
	var keycode = keycode.split('|');
	var cid = req.body.cid * 1;
	if(keycode[2]){		//验证密钥	
		var mysql = require('mysql');
		var client = mysql.createConnection(myDb);
		client.connect();
		client.query("use " + myDb.DbName);
		var async = require('async');
			async.waterfall([
				function(callback){
					//console.log('SELECT `uid`,`fid` FROM 360_member_relation WHERE (`uid`= '+keycode[2]+' or `fid` = '+keycode[2]+')  and `state`=2');
					client.query('SELECT `uid`,`fid`,`uViewTime`,`fViewTime` FROM 360_member_relation WHERE (`uid`= '+keycode[2]+' or `fid` = '+keycode[2]+')  and `state`=2',
						function selectCb(err, returnData){
							//console.log(returnData);
							//console.log(returnData.length);
							if(returnData.length)
								callback(null,returnData);
							else
								res.send(JSON.stringify({code:'2',data:[]}));
						}
					)
				},
				function(a1 , callback){
					var fid = "";
					for(var i in a1) {
						if(a1[i].fid != keycode[2])
							fid += "," + a1[i].fid;
						if(a1[i].uid != keycode[2])
							fid += "," + a1[i].uid;				
					}
					fid = fid.substring(1);
					callback(null,fid);
				},
				//返回
				function(b2 , callback){
					var client = mysql.createConnection(myDb);
					client.connect();
					client.query("use " + myDb.DbName);
					console.log('SELECT  `id`,`headPath`,`nickName` FROM 360_member WHERE id in('+b2+')');
					client.query('SELECT `id`,`headPath`,`nickName` FROM 360_member WHERE id in('+b2+')',
						function selectCb(err, returnData){
							if(err)
								res.send(JSON.stringify({code:'2'}));
							//if(returnData.length){
								var fData = {};
								fData.code = "1";
								fData.data = returnData;
								callback(null,fData);
							//}
							//else{
							//	callback(null,false);
							//}
						}
					)
				},
				//获取消息数 !!!未写
				function(c3 , callback){
					console.log(c3);
					res.send(JSON.stringify(c3));
				}],
				function (err, result) {
					return;
				}
			)
	}
	else
		res.send(JSON.stringify({code:'2'}));
}